//
//  downMenu.m
//  DZZTravel
//
//  Created by 赵国栋 on 16/7/14.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "downMenu.h"



@interface downMenu ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic ,strong)UICollectionView *collect;

@property (nonatomic ,strong)NSMutableArray *cityArray;


@property (nonatomic ,strong)cityName *city;
@end

@implementation downMenu


- (NSMutableArray *)cityArray{
    if (!_cityArray) {
        _cityArray = [NSMutableArray array];
    }
    return _cityArray;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creatView];
   [self addSubview:_collect];
    }
    return self;
}


//数据请求
- (void)dataSource{
    [RequestManager fectDataWithUrl:KcityNameUrl parameter:@{} method:@"GET" compelet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
        for (NSDictionary *dicc in dic[@"content"]) {
       
            self.city =[cityName new];
            [self.city setValuesForKeysWithDictionary:dicc];
           [self.cityArray addObject:self.city];
            
        }
        
    } upDateUI:^{
        [self.collect reloadData];
    }];
  
    
}

//创建collection
- (void)creatView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置大小
    layout.itemSize = CGSizeMake(50, 50);
    //设置间距
    layout.sectionInset = UIEdgeInsetsMake(self.frame.size.height/4,5,0,5);
   
    _collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0,self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    _collect.dataSource = self;
    _collect.delegate = self;
    _collect.scrollEnabled = NO;
    
    [_collect registerNib:[UINib nibWithNibName:@"MenuCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MenuCollectionViewCell"];
    _collect.backgroundColor = [UIColor whiteColor];
    
    [self dataSource];
    
}



#pragma mark-----------------collection-------------------


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.cityArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCollectionViewCell" forIndexPath:indexPath];
    cell.name.text = [self.cityArray[indexPath.row] name];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    cityName *city = self.cityArray[indexPath.row];
    [self.delegate jump:city];
  

}







@end
